package com.it.config;

import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class WebXmlConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     *  配置父容器资源,然后初始化父容器
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    /**
     * 配置web层相关的子容器资源
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //注册核心调度器功能
        super.onStartup(servletContext);
        //注册过滤器
        FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class);
        //定义过滤规则
        //EnumSet<DispatcherType> fileType=EnumSet.allOf();
        characterEncodingFilter.addMappingForUrlPatterns(null,true,"/*");
    }

    /**
     * 设置核心调度器的拦截规则
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
